package com.sun.controller;

import cn.hutool.json.JSONObject;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.annotation.Log;
import com.sun.common.Result;
import com.sun.common.ResultCodeEnum;
import com.sun.entity.EnhanceProduct;
import com.sun.listen.SysLogEvent;
import com.sun.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @SentinelResource(value = "orderList")
    @GetMapping("/list")
    public List<EnhanceProduct> selectOrderList() {
       return  orderService.list();
    }


    /**
     * 根据相关信息进行数据信息的查询
     * @param enhanceProduct
     * @return
     */
    @Log
    @PostMapping("/get")
    public Result<List<EnhanceProduct>> getProductList(@Valid @RequestBody EnhanceProduct enhanceProduct){
        Result<List<EnhanceProduct>> result = new Result();
        List<EnhanceProduct> productList = orderService.list(new QueryWrapper<EnhanceProduct>().like("pname", enhanceProduct.getName()));
        result.setData(productList);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }


    /**
     *  监听程序
     * @param event
     */
    @Async
    @EventListener(SysLogEvent.class)
    public void saveSysLog(SysLogEvent event) {
        Object returnValue = event.getReturnValue();
        String jsonString = JSON.toJSONString(returnValue);
        event.setReturnValue(jsonString);
        orderService.saveSysLog(event);
    }


}
